

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>8.2. Fast atom neighbor lookup using a KD tree (implemented in C++) — MDAnalysis.KDTree.NeighborSearch &mdash; MDAnalysis v0.7.5-devel documentation</title>
    <link rel="stylesheet" href="../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.7.5-devel',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within MDAnalysis v0.7.5-devel documentation"
          href="../../_static/opensearch.xml"/>
    <link rel="shortcut icon" href="../../_static/mdanalysis-logo.ico"/>
    <link rel="top" title="MDAnalysis v0.7.5-devel documentation" href="../../index.html" />
    <link rel="up" title="8. KDTree module" href="../KDTree_modules.html" />
    <link rel="prev" title="8.1. KDTree — MDAnalysis.KDTree.KDTree" href="KDTree.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="KDTree.html" title="8.1. KDTree — MDAnalysis.KDTree.KDTree"
             accesskey="P">previous</a> |</li>
        <li><a href="../../index.html">MDAnalysis v0.7.5-devel documentation</a> &raquo;</li>
          <li><a href="../KDTree_modules.html" accesskey="U">8. KDTree module</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/mdanalysis-logo-200x150.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="KDTree.html"
                        title="previous chapter">8.1. KDTree &#8212; <tt class="docutils literal docutils literal"><span class="pre">MDAnalysis.KDTree.KDTree</span></tt></a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/documentation_pages/KDTree/NeighborSearch.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <span class="target" id="module-MDAnalysis.KDTree.NeighborSearch"></span><div class="section" id="fast-atom-neighbor-lookup-using-a-kd-tree-implemented-in-c-mdanalysis-kdtree-neighborsearch">
<h1>8.2. Fast atom neighbor lookup using a KD tree (implemented in C++) &#8212; <a class="reference internal" href="#module-MDAnalysis.KDTree.NeighborSearch" title="MDAnalysis.KDTree.NeighborSearch"><tt class="xref py py-mod docutils literal"><span class="pre">MDAnalysis.KDTree.NeighborSearch</span></tt></a><a class="headerlink" href="#fast-atom-neighbor-lookup-using-a-kd-tree-implemented-in-c-mdanalysis-kdtree-neighborsearch" title="Permalink to this headline">¶</a></h1>
<p>One can use KD-Trees to speed up searches. MDAnalysis uses Thomas
Hamelryck&#8217;s C++ implementation from <a class="reference external" href="http://biopython.org">Biopython</a>. The following are
fairly technical descriptions of the available classes.</p>
<p>If you know that you are using a specific selection repeatedly then
may be faster to explicitly build the selection using the
<a class="reference internal" href="#MDAnalysis.KDTree.NeighborSearch.AtomNeighborSearch" title="MDAnalysis.KDTree.NeighborSearch.AtomNeighborSearch"><tt class="xref py py-class docutils literal"><span class="pre">AtomNeighborSearch</span></tt></a> class instead of using MDAnalysis
selections.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">MDAnalysis.KDTree.NeighborSearch</span> <span class="kn">as</span> <span class="nn">NS</span>

<span class="n">u</span> <span class="o">=</span> <span class="n">Universe</span><span class="p">(</span><span class="n">psf</span><span class="p">,</span><span class="n">dcd</span><span class="p">)</span>
<span class="n">water</span> <span class="o">=</span> <span class="n">u</span><span class="o">.</span><span class="n">selectAtoms</span><span class="p">(</span><span class="s">&#39;name OH2&#39;</span><span class="p">)</span>
<span class="n">protein</span> <span class="o">=</span> <span class="n">u</span><span class="o">.</span><span class="n">selectAtoms</span><span class="p">(</span><span class="s">&#39;protein&#39;</span><span class="p">)</span>

<span class="c"># when analyzing a trajectory, carry out the next two steps</span>
<span class="c"># for every frame</span>
<span class="n">ns_w</span> <span class="o">=</span> <span class="n">NS</span><span class="o">.</span><span class="n">AtomNeighborSearch</span><span class="p">(</span><span class="n">water</span><span class="p">)</span>
<span class="n">solvation_shell</span> <span class="o">=</span> <span class="n">ns_w</span><span class="o">.</span><span class="n">search_list</span><span class="p">(</span><span class="n">protein</span><span class="p">,</span><span class="mf">4.0</span><span class="p">)</span>  <span class="c"># water oxygens within 4A of protein</span>
</pre></div>
</div>
<dl class="class">
<dt id="MDAnalysis.KDTree.NeighborSearch.AtomNeighborSearch">
<em class="property">class </em><tt class="descclassname">MDAnalysis.KDTree.NeighborSearch.</tt><tt class="descname">AtomNeighborSearch</tt><big>(</big><em>atom_list</em>, <em>bucket_size=10</em><big>)</big><a class="headerlink" href="#MDAnalysis.KDTree.NeighborSearch.AtomNeighborSearch" title="Permalink to this definition">¶</a></dt>
<dd><p>This class can be used for two related purposes:</p>
<p>1. To find all atoms/residues/segments within radius
of a given query position.</p>
<p>2. To find all atoms/residues/segments that are within
a fixed radius of each other.</p>
<p>AtomNeighborSearch makes use of the KDTree C++ module, so it&#8217;s fast.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><dl class="first last docutils">
<dt><em>atom_list</em></dt>
<dd><p class="first last">list of atoms (<a class="reference internal" href="../core/AtomGroup.html#MDAnalysis.core.AtomGroup.Atom" title="MDAnalysis.core.AtomGroup.Atom"><tt class="xref py py-class docutils literal"><span class="pre">Atom</span></tt></a>) or a
<a class="reference internal" href="../core/AtomGroup.html#MDAnalysis.core.AtomGroup.AtomGroup" title="MDAnalysis.core.AtomGroup.AtomGroup"><tt class="xref py py-class docutils literal"><span class="pre">AtomGroup</span></tt></a>.
This list is used in the queries. It can contain atoms from different structures.</p>
</dd>
<dt><em>bucket_size</em></dt>
<dd><p class="first last">bucket size of KD tree. You can play around with this to optimize speed if you feel like it.</p>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="MDAnalysis.KDTree.NeighborSearch.AtomNeighborSearch.search">
<tt class="descname">search</tt><big>(</big><em>center</em>, <em>radius</em>, <em>level='A'</em><big>)</big><a class="headerlink" href="#MDAnalysis.KDTree.NeighborSearch.AtomNeighborSearch.search" title="Permalink to this definition">¶</a></dt>
<dd><p>Neighbor search.</p>
<p>Return all atoms/residues/segments
that have at least one atom within <em>radius</em> of <em>center</em>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><dl class="first last docutils">
<dt><em>center</em></dt>
<dd><p class="first last">numpy array of shape 3  (cartesian coordinates)</p>
</dd>
<dt><em>radius</em></dt>
<dd><p class="first last">float</p>
</dd>
<dt><em>level</em></dt>
<dd><p class="first last">char (A, R, S); what entitity level is returned
(e.g. atoms or residues) is determined by level (A=atoms,
R=residues, S=Segments).</p>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
<p>In order to obtain the coordinates for the <em>center</em> argument
from a <a class="reference internal" href="../core/AtomGroup.html#MDAnalysis.core.AtomGroup.AtomGroup" title="MDAnalysis.core.AtomGroup.AtomGroup"><tt class="xref py py-class docutils literal"><span class="pre">AtomGroup</span></tt></a> one can
simply provide the output of the
<a class="reference internal" href="../core/AtomGroup.html#MDAnalysis.core.AtomGroup.AtomGroup.centroid" title="MDAnalysis.core.AtomGroup.AtomGroup.centroid"><tt class="xref py py-class docutils literal"><span class="pre">centroid</span></tt></a> or
<a class="reference internal" href="../core/AtomGroup.html#MDAnalysis.core.AtomGroup.AtomGroup.centerOfMass" title="MDAnalysis.core.AtomGroup.AtomGroup.centerOfMass"><tt class="xref py py-class docutils literal"><span class="pre">centerOfMass</span></tt></a>
functions.</p>
</dd></dl>

<dl class="method">
<dt id="MDAnalysis.KDTree.NeighborSearch.AtomNeighborSearch.search_all">
<tt class="descname">search_all</tt><big>(</big><em>radius</em>, <em>level='A'</em><big>)</big><a class="headerlink" href="#MDAnalysis.KDTree.NeighborSearch.AtomNeighborSearch.search_all" title="Permalink to this definition">¶</a></dt>
<dd><p>All neighbor search.</p>
<p>Search all entities that have atoms pairs within
<em>radius</em>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><dl class="first last docutils">
<dt><em>radius</em></dt>
<dd><p class="first last">float</p>
</dd>
<dt><em>level</em></dt>
<dd><p class="first last">char (A, R, S); what entitity level is returned
(e.g. atoms or residues) is determined by level (A=atoms,
R=residues, S=Segments).</p>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="MDAnalysis.KDTree.NeighborSearch.AtomNeighborSearch.search_list">
<tt class="descname">search_list</tt><big>(</big><em>other</em>, <em>radius</em>, <em>level='A'</em><big>)</big><a class="headerlink" href="#MDAnalysis.KDTree.NeighborSearch.AtomNeighborSearch.search_list" title="Permalink to this definition">¶</a></dt>
<dd><p>Search neighbours near all atoms in atoms.</p>
<p>Returns all atoms/residues/segments that contain atoms that are
within <em>radius</em> of any other atom listed in <em>other</em>, i.e. &#8220;find all A
within R of B&#8221; where A are the atoms used for setting up the
AtomNeighborSearch and B are the other atoms.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><dl class="first last docutils">
<dt><em>other</em></dt>
<dd><p class="first last"><a class="reference internal" href="../core/AtomGroup.html#MDAnalysis.core.AtomGroup.AtomGroup" title="MDAnalysis.core.AtomGroup.AtomGroup"><tt class="xref py py-class docutils literal"><span class="pre">AtomGroup</span></tt></a> or list of <a class="reference internal" href="../core/AtomGroup.html#MDAnalysis.core.AtomGroup.Atom" title="MDAnalysis.core.AtomGroup.Atom"><tt class="xref py py-class docutils literal"><span class="pre">Atom</span></tt></a> instances</p>
</dd>
<dt><em>radius</em></dt>
<dd><p class="first last">float</p>
</dd>
<dt><em>level</em></dt>
<dd><p class="first last">char (A, R, S); what entitity level is returned
(e.g. atoms or residues) is determined by level (A=atoms,
R=residues, S=Segments).</p>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="MDAnalysis.KDTree.NeighborSearch.CoordinateNeighborSearch">
<em class="property">class </em><tt class="descclassname">MDAnalysis.KDTree.NeighborSearch.</tt><tt class="descname">CoordinateNeighborSearch</tt><big>(</big><em>coordinates</em>, <em>bucket_size=10</em><big>)</big><a class="headerlink" href="#MDAnalysis.KDTree.NeighborSearch.CoordinateNeighborSearch" title="Permalink to this definition">¶</a></dt>
<dd><p>This class can be used for two related purposes:</p>
<p>1. To find all indices of a coordinate list within radius
of a given query position.</p>
<p>2. To find all indices of a coordinate list that are within
a fixed radius of each other.</p>
<p>CoordinateNeighborSearch makes use of the KDTree C++ module, so it&#8217;s fast.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><dl class="first last docutils">
<dt><em>coordinates</em></dt>
<dd><p class="first last">list of N coordinates (Nx3 numpy array)</p>
</dd>
<dt><em>bucket_size</em></dt>
<dd><p class="first last">bucket size of KD tree. You can play around with this to
optimize speed if you feel like it.</p>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="MDAnalysis.KDTree.NeighborSearch.CoordinateNeighborSearch.search">
<tt class="descname">search</tt><big>(</big><em>center</em>, <em>radius</em>, <em>distances=False</em><big>)</big><a class="headerlink" href="#MDAnalysis.KDTree.NeighborSearch.CoordinateNeighborSearch.search" title="Permalink to this definition">¶</a></dt>
<dd><p>Neighbor search.</p>
<p>Return all indices in the coordinates list that have at least
one atom within <em>radius</em> of <em>center</em>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><ul class="first last">
<li><dl class="first docutils">
<dt>center</dt>
<dd><p class="first last">numpy array</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>radius</dt>
<dd><p class="first last">float</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>distances</dt>
<dd><p class="first last">bool  <tt class="xref docutils literal"><span class="pre">True</span></tt>: return (indices,distances); <tt class="xref docutils literal"><span class="pre">False</span></tt>: return indices only</p>
</dd>
</dl>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="MDAnalysis.KDTree.NeighborSearch.CoordinateNeighborSearch.search_all">
<tt class="descname">search_all</tt><big>(</big><em>radius</em>, <em>distances=False</em><big>)</big><a class="headerlink" href="#MDAnalysis.KDTree.NeighborSearch.CoordinateNeighborSearch.search_all" title="Permalink to this definition">¶</a></dt>
<dd><p>All neighbor search.</p>
<p>Return all index pairs corresponding to coordinates within the <em>radius</em>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><dl class="first last docutils">
<dt><em>radius</em></dt>
<dd><p class="first last">float</p>
</dd>
<dt><em>distances</em></dt>
<dd><p class="first last">bool  <tt class="xref docutils literal"><span class="pre">True</span></tt>:  return (indices,distances); <tt class="xref docutils literal"><span class="pre">False</span></tt>: return indices only
[<tt class="xref docutils literal"><span class="pre">False</span></tt>]</p>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="MDAnalysis.KDTree.NeighborSearch.CoordinateNeighborSearch.search_list">
<tt class="descname">search_list</tt><big>(</big><em>centers</em>, <em>radius</em><big>)</big><a class="headerlink" href="#MDAnalysis.KDTree.NeighborSearch.CoordinateNeighborSearch.search_list" title="Permalink to this definition">¶</a></dt>
<dd><p>Search neighbours near all centers.</p>
<p>Returns all indices that are within <em>radius</em> of any center listed in
<em>centers</em>, i.e. &#8220;find all A within R of B&#8221; where A are the
coordinates used for setting up the CoordinateNeighborSearch and B
are the centers.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><dl class="first last docutils">
<dt><em>centers</em></dt>
<dd><p class="first last">Mx3 numpy array of M centers</p>
</dd>
<dt><em>radius</em></dt>
<dd><p class="first last">float</p>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="KDTree.html" title="8.1. KDTree — MDAnalysis.KDTree.KDTree"
             >previous</a> |</li>
        <li><a href="../../index.html">MDAnalysis v0.7.5-devel documentation</a> &raquo;</li>
          <li><a href="../KDTree_modules.html" >8. KDTree module</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2005-2011, Naveen Michaud-Agrawal, Elizabeth J. Denning, Joshua Adelman,
    Christian Beckstein (logo), David Caplan, Jan Domański, Philip Fowler,
    Joseph Goose, Benjamin Hall, Danny Parton, Tyler Reddy, Paul Rigor,
    and Oliver Beckstein.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>
  </body>
</html>